package com.zzyl.config;

import com.aliyun.oss.OSS;
import com.zzyl.properties.AliOssConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class OssAliyunAutoConfig {

    @Autowired
    AliOssConfigProperties aliOssConfigProperties;

    @Bean
    public OSS ossClient() throws Exception {
//        log.info("-----------------开始创建OSSClient--------------------");
//        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
//        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(aliOssConfigProperties.getEndpoint(), credentialsProvider);
//        //判断容器是否存在,不存在就创建
//        if (!ossClient.doesBucketExist(aliOssConfigProperties.getBucketName())) {
//            ossClient.createBucket(aliOssConfigProperties.getBucketName());
//            CreateBucketRequest createBucketRequest = new CreateBucketRequest(aliOssConfigProperties.getBucketName());
//            //设置问公共可读
//            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
//            ossClient.createBucket(createBucketRequest);
//        }
//
//        //添加客户端访问日志
//        SetBucketLoggingRequest request = new SetBucketLoggingRequest(aliOssConfigProperties.getBucketName());
//        // 设置存放日志文件的存储空间。
//        request.setTargetBucket(aliOssConfigProperties.getBucketName());
//        // 设置日志文件存放的目录。
//        request.setTargetPrefix(aliOssConfigProperties.getBucketName());
//        ossClient.setBucketLogging(request);
//
//        log.info("-----------------结束创建OSSClient--------------------");
//        return ossClient;
        return null;
    }


}
